package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.yl.auth.model.entity.Dizhi;
import com.yl.auth.service.DizhiService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-06-15
 */
@Api(tags = "养老护理-地址管理")
@RestController
@RequestMapping("/admin/dizhi")
public class DizhiController {
    @Resource
    private DizhiService dizhiService;


    @PostMapping("/generate-qrcode")
    @ApiOperation(value = "生成二维码")
    public Result generateQRCode(@RequestBody Dizhi dizhi) {
        String data = dizhi.getQr(); // 获取用于生成二维码的数据

        // 设置二维码参数
        int width = 300;
        int height = 300;
        String format = "png";
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        // 生成二维码
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix;
        try {
            matrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
            return Result.ok("Failed to generate QR Code");
        }

        // 生成一个随机的文件名，可以使用UUID或者当前时间戳等方式
        String fileName = UUID.randomUUID().toString() + ".png";
        // 指定保存路径和文件名
        String saveDirectory = "E:/lnoa2209java/codeylhomes/public/qr2023/";
        String filePath = saveDirectory + fileName;
        File qrCodeFile = new File(filePath);
        try {
            Path path = Paths.get(qrCodeFile.getAbsolutePath());
            MatrixToImageWriter.writeToPath(matrix, format, path);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.ok("Failed to save QR Code");
        }

        // 获取保存路径的后半部分
        String databasePath = filePath.substring(filePath.indexOf("/qr2023/"));

        // 将保存路径存储到数据库
        Dizhi dizhi1 = new Dizhi();
        dizhi1.setQr(databasePath);
        dizhi1.setDizhiname(dizhi.getDizhiname());
        dizhiService.save(dizhi1);

        return Result.ok("QR Code generated and saved successfully");
    }


    @PostMapping("/save")
    @ApiOperation(value = "添加地址")
    public Result savesss(@RequestBody @ApiParam("地址实体类")Dizhi dizhi){
        dizhiService.save(dizhi);
        return Result.ok(dizhi);
    }

    /**
     * 地址管理分页查询
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取全部地址分页")
    @GetMapping("/getdizhi/{page}/{limit}")
    public Result getdizhi(@PathVariable @ApiParam("当前页") Long page, @PathVariable @ApiParam("总条数") Long limit) {
        Page<Dizhi> dizhiPage = new Page<>(page, limit);
        // 添加排序参数，按照ID的倒序排列
        dizhiPage.setDesc("id");
        IPage<Dizhi> pageModel = dizhiService.page(dizhiPage);
        return Result.ok(pageModel);
    }




    /**
     * 地址管理根据Id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除地址")
    @DeleteMapping("/deleteById/{id}")
    public Result delete(@PathVariable Integer id){
        dizhiService.removeById(id);
        return Result.ok();
    }

    /**
     * 地址管理回显
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID回显")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Integer id) {
        Dizhi dizhi = dizhiService.getById(id);
        return Result.ok(dizhi);
    }

    /**
     * 地址管理修改
     * @param dizhi
     * @return
     */
    @ApiOperation(value = "修改地址")
    @PutMapping("/update")
    public Result updateById(@RequestBody Dizhi dizhi){
        dizhiService.updateById(dizhi);
        return Result.ok();
    }
    @GetMapping("/getyexundizhi")
    @ApiOperation(value = "获取全部地址不分页夜巡管理回显调用")
    public Result getyexundizhi(){
        List<Dizhi> dizhiList = dizhiService.list();
        return Result.ok(dizhiList);
    }
}

